﻿using System;
using System.IO;
using System.Xml;

namespace Common.XmlHandle
{
    public class XmlConfig
    {
        private XmlDocument xdoc;
        private XmlNode root;
        private string xmlPath;
        //private ClassConfig config = new ClassConfig();

        public XmlConfig(string path)
        {
            xmlPath = path;
            try
            {
                xdoc = new XmlDocument();
                xdoc.Load(path);
                root = xdoc.SelectSingleNode("Config");
            }
            catch (DirectoryNotFoundException)
            {
                Directory.CreateDirectory(path.Substring(0, path.LastIndexOf(@"\")));
            }
            catch (FileNotFoundException)
            {
                XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xdoc.AppendChild(dec);
                xdoc.AppendChild(xdoc.CreateElement("Config"));
                xdoc.Save(path);
            }
            catch (XmlException)
            {
                xdoc = new XmlDocument();
                XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xdoc.AppendChild(dec);
                xdoc.AppendChild(xdoc.CreateElement("Config"));
                xdoc.Save(path);
            }
            catch
            {
                throw;
            }
        }
        
        public ClassConfig GetConfig()
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Config");
            ClassConfig b = this.getConfigFromNode();
            if(b != null)
            {
                return b;
            }
            else
            {
                return null;
            }
            //return this.getConfigFromNode();
        }

        /// <summary>
        /// 判断给定的guid是不是本机用户的guid
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public bool IsTheOne(Guid guid)
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Config");
            XmlNodeList nodes = root.ChildNodes;
            XmlElement node = nodes[0] as XmlElement;
            Guid localGuid = new Guid(node.GetAttribute("Guid"));
            return localGuid.Equals(guid) ? true : false;
        }

        public void SetConfig(ClassConfig config)
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Config");
            if (config != null)
            {
                SetXML(config);
            }
        }

        public bool IsAdmin()
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Config");
            XmlNode node = root.FirstChild;
            XmlNodeList nods = node.ChildNodes;
            return Boolean.Parse(nods[3].InnerText);
        }

        private void SetXML(ClassConfig config)
        {
            xdoc.Load(xmlPath);
            root = xdoc.SelectSingleNode("Config");
            try
            {
                XmlNodeList nodeList = root.ChildNodes;
                if(nodeList.Count == 0)
                {
                    addNode(config);
                }
                else
                {
                    editNode(config);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private ClassConfig getConfigFromNode()
        {
            XmlNode node = root.FirstChild;
            if(node != null)
            {
                ClassConfig b = new ClassConfig();
                XmlElement nod = (XmlElement)node;
                b.UserGuid = new Guid(nod.GetAttribute("Guid"));
                b.State = Boolean.Parse(nod.GetAttribute("State"));
                XmlNodeList nos = node.ChildNodes;
                b.UserName = nos[0].InnerText;
                b.UserLicense = (License)Enum.Parse(typeof(License), nos[1].InnerText);
                b.IsServer = Boolean.Parse(nos[2].InnerText);
                b.IsAdmin = Boolean.Parse(nos[3].InnerText);
                b.IsCustomize = Boolean.Parse(nos[4].InnerText);
                b.IsQueryOnly = Boolean.Parse(nos[5].InnerText);
                b.InPath = nos[6].InnerText;
                b.OutPathFromString(nos[7].InnerText);
                return b;
            }
            return null;
        }

        private void editNode(ClassConfig config)
        {
            XmlNode node = root.FirstChild;
            XmlElement xmlEle = (XmlElement)node;
            Guid local_Guid = new Guid(xmlEle.GetAttribute("Guid"));
            if (local_Guid == Guid.Empty | config.UserGuid == local_Guid)//可临时修改为其它用户，防止永久修改
            {
                node.Attributes["State"].InnerText = config.State.ToString();
                XmlNodeList xlist = node.ChildNodes;
                xlist[0].InnerText = config.UserName;
                xlist[1].InnerText = config.UserLicense.ToString();
                xlist[2].InnerText = config.IsServer.ToString();
                xlist[3].InnerText = config.IsAdmin.ToString();
                xlist[4].InnerText = config.IsCustomize.ToString();
                xlist[5].InnerText = config.IsQueryOnly.ToString();
                xlist[6].InnerText = config.InPath;
                xlist[7].InnerText = config.OutPath2String();
                xdoc.Save(this.xmlPath);
            }
        }

        private void addNode(ClassConfig config)
        {
            if(config != null)
            {
                root.RemoveAll();
                XmlElement node = xdoc.CreateElement("User");
                node.SetAttribute("Guid", config.UserGuid.ToString());
                node.SetAttribute("State", config.State.ToString());
                root.AppendChild(node);
                XmlElement ele = xdoc.CreateElement("UserName");
                ele.InnerText = config.UserName;
                node.AppendChild(ele);
                ele = xdoc.CreateElement("UserLicence");
                ele.InnerText = config.UserLicense.ToString();
                node.AppendChild(ele);
                ele = xdoc.CreateElement("IsServer");
                ele.InnerText = config.IsServer.ToString();
                node.AppendChild(ele);
                ele = xdoc.CreateElement("IsAdmin");
                ele.InnerText = config.IsAdmin.ToString();
                node.AppendChild(ele);
                ele = xdoc.CreateElement("IsCustomize");
                ele.InnerText = config.IsCustomize.ToString();
                node.AppendChild(ele);
                ele = xdoc.CreateElement("IsQueryOnly");
                ele.InnerText = config.IsQueryOnly.ToString();
                node.AppendChild(ele);
                ele = xdoc.CreateElement("InPath");
                ele.InnerText = config.InPath;
                node.AppendChild(ele);
                ele = xdoc.CreateElement("OutPath");
                ele.InnerText = config.OutPath2String();
                node.AppendChild(ele);
                xdoc.Save(this.xmlPath);
            }
        }
    }
}
